package com.mycompany.app.hr.hrinterface.importing.csvjdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mycompany.app.hr.hrinterface.exception.CsvImportException;

public class CsvJdbcImporter implements ICsvJdbcImporter {
    private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(CsvJdbcImporter.class);

    private Connection csvConnection;
    
    

    public Connection getCsvConnection() {
    
        return csvConnection;
    }

    public void setCsvConnection(Connection csvConnection) {
    
        this.csvConnection = csvConnection;
    }

    public void importCsvFile(String csvFileName) throws CsvImportException {

        Statement stmt = null;
        ResultSet resultSet = null;

        try {
            stmt = csvConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                    ResultSet.CLOSE_CURSORS_AT_COMMIT);

            resultSet = stmt.executeQuery("select * from " + csvFileName);
            
            ResultSetMetaData mt = resultSet.getMetaData();
            log.info("column count " + mt.getColumnCount());
            ArrayList<String> validColumnArray=new ArrayList<String>(mt.getColumnCount());
            for(int i=1; i<=mt.getColumnCount(); i++) {
                String columnName=mt.getColumnName(i);
                log.info("column name " + columnName);
                if(columnName!=null&& !columnName.equals(""))
                    validColumnArray.add(columnName);
            }
            
            Object[] validColumns=(Object[])validColumnArray.toArray();
            while(resultSet.next()) {
                
                for(int i=0;i<validColumns.length;i++)
                {
                    System.out.print(validColumns[i]+":"+resultSet.getString((String)validColumns[i])+"    ");
                }
                System.out.println();
                
            }
            
            
            
        } catch (SQLException e) {
            releaseResourceQuietly(csvConnection, resultSet);
            e.printStackTrace();
            throw new CsvImportException();
        }
        finally{
            releaseResourceQuietly(csvConnection, resultSet);
        }

    }

    public void initializeMetaData(String csvFileName) {

        // TODO Auto-generated method stub

    }

    /**
     * release the opened interface file resource.
     * 
     * @Description:
     */
    private void releaseResourceQuietly(Connection conn, ResultSet resultSet) {

        if (resultSet != null)
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }

        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
    }

}
